package main

import (
	"errors"
	"fmt"
	"log"
	"runtime/debug"
)

func main() {
	panicCauseDefer()

	//println("main start")
	//user()
	//println("main end")
}

func user() {
	println("user start")
	goods()
	println("user end")
}
func goods() {
	defer func() {
		if e := recover(); e != nil {
			//fmt.Println("recover the panic:", e)
			fmt.Println("奔溃httprequest recover the panic:", e, string(debug.Stack()))
			log.Println("奔溃httprequest recover the panic:", e, string(debug.Stack()))
		}
	}()
	
	println("goods start")
	//panic("在 goods 中触发了 panic")
	
	panic(errors.New("在 goods 中触发了 panic"))
	
	info()               //Unreachable code
	println("goods end") //Unreachable code
}
func info() {
	println("info start")
	println("info end")
}

//这里我们讨论下log.Fatal和panic对程序的影响
func foo() {
	defer func () { log.Print("3333")} ()
	panic("4444")
}

func panicCauseDefer() {
	log.Print("1111")
	defer func () { log.Print("2222")} ()
	foo()
	log.Print("9999")
}